package zzs.generalframework.console.core.parameter.commonparameter.censusregistertype.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.parameter.commonparameter.censusregistertype.CensusRegisterTypeBO;
import zzs.generalframework.common.core.parameter.commonparameter.censusregistertype.CensusRegisterTypeInfo;
import zzs.generalframework.common.core.parameter.commonparameter.censusregistertype.CensusRegisterTypePO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.parameter.commonparameter.censusregistertype.dao.ICensusRegisterTypeDao;
import zzs.generalframework.console.core.parameter.commonparameter.censusregistertype.service.ICensusRegisterTypeService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.censusregistertype.service.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.parameter.commonparameter.censusregistertype.service.impl.censusRegisterTypeServiceImpl")
public class CensusRegisterTypeServiceImpl implements ICensusRegisterTypeService
{

	private Logger logger = LoggerFactory.getLogger(CensusRegisterTypeServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	ICensusRegisterTypeDao censusRegisterTypeDao;

	@Override
	public ConsoleReturnBO saveCensusRegisterType(CensusRegisterTypeBO censusRegisterTypeBO)
	{
		logger.debug("====保存户口类别信息开始====");
		logger.debug("====保存户口类别信息.入参:CensusRegisterTypeBO=[{}]====", censusRegisterTypeBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		CensusRegisterTypePO censusRegisterTypePO = new CensusRegisterTypePO();
		copyCensusRegisterTypeBO2CensusRegisterTypePO(censusRegisterTypeBO, censusRegisterTypePO);
		Boolean insertResult = censusRegisterTypeDao.insert(censusRegisterTypePO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存户口类别信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存户口类别信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveCensusRegisterType(List<CensusRegisterTypeBO> censusRegisterTypeBOList) throws ConsoleException
	{
		logger.info("====保存户口类别信息列表开始====");
		logger.debug("====保存户口类别信息列表.入参:CensusRegisterTypeBOList=[{}]====", censusRegisterTypeBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (CensusRegisterTypeBO itemBO : censusRegisterTypeBOList)
		{
			ConsoleReturnBO itemResult = saveCensusRegisterType(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====保存户口类别信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存户口类别信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getCensusRegisterType(CensusRegisterTypeInfo censusRegisterTypeInfo)
	{
		logger.info("====获取户口类别信息开始====");
		logger.debug("====获取户口类别信息.入参:CensusRegisterTypeInfo=[{}]====", censusRegisterTypeInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<CensusRegisterTypePO> censusRegisterTypePOList = censusRegisterTypeDao.select(censusRegisterTypeInfo);
		if (censusRegisterTypePOList == null || censusRegisterTypePOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (censusRegisterTypePOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			CensusRegisterTypeBO censusRegisterTypeBO = new CensusRegisterTypeBO();
			copyCensusRegisterTypePO2CensusRegisterTypeBO(censusRegisterTypePOList.get(0), censusRegisterTypeBO);
			returnConsoleReturnBO.setReturnObj(censusRegisterTypeBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			CensusRegisterTypeBO censusRegisterTypeBO = new CensusRegisterTypeBO();
			copyCensusRegisterTypePO2CensusRegisterTypeBO(censusRegisterTypePOList.get(0), censusRegisterTypeBO);
			returnConsoleReturnBO.setReturnObj(censusRegisterTypeBO);
		}
		logger.debug("====获取户口类别信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取户口类别信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listCensusRegisterType(CensusRegisterTypeInfo censusRegisterTypeInfo)
	{
		logger.info("====获取户口类别信息列表开始====");
		logger.debug("====获取户口类别信息列表.入参:CensusRegisterTypeInfo=[{}]====", censusRegisterTypeInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<CensusRegisterTypePO> censusRegisterTypePOList = censusRegisterTypeDao.select(censusRegisterTypeInfo);
		if (censusRegisterTypePOList == null || censusRegisterTypePOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<CensusRegisterTypeBO> resultList = new ArrayList<CensusRegisterTypeBO>();
			for (CensusRegisterTypePO itemPO : censusRegisterTypePOList)
			{
				CensusRegisterTypeBO itemBO = new CensusRegisterTypeBO();
				copyCensusRegisterTypePO2CensusRegisterTypeBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取户口类别信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取户口类别信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateCensusRegisterType(CensusRegisterTypeBO censusRegisterTypeBO)
	{
		logger.info("====更新户口类别信息开始====");
		logger.debug("====更新户口类别信息.入参:CensusRegisterTypeBO=[{}]====", censusRegisterTypeBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		CensusRegisterTypePO censusRegisterTypePO = new CensusRegisterTypePO();
		copyCensusRegisterTypeBO2CensusRegisterTypePO(censusRegisterTypeBO, censusRegisterTypePO);
		Boolean updateResult = censusRegisterTypeDao.update(censusRegisterTypePO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新户口类别信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新户口类别信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateCensusRegisterType(List<CensusRegisterTypeBO> censusRegisterTypeBOList) throws ConsoleException
	{
		logger.info("====更新户口类别信息列表开始====");
		logger.debug("====更新户口类别信息列表.入参:List<CensusRegisterTypeBO>=[{}]====", censusRegisterTypeBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (CensusRegisterTypeBO itemBO : censusRegisterTypeBOList)
		{
			ConsoleReturnBO itemResult = updateCensusRegisterType(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====更新户口类别信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新户口类别信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeCensusRegisterType(CensusRegisterTypeBO censusRegisterTypeBO)
	{
		logger.info("====删除户口类别信息开始====");
		logger.debug("====删除户口类别信息.入参:CensusRegisterTypeBO＝[{}]======", censusRegisterTypeBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		CensusRegisterTypePO censusRegisterTypePO = new CensusRegisterTypePO();
		copyCensusRegisterTypeBO2CensusRegisterTypePO(censusRegisterTypeBO, censusRegisterTypePO);
		Boolean deleteResult = censusRegisterTypeDao.delete(censusRegisterTypePO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除户口类别信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除户口类别信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeCensusRegisterType(List<CensusRegisterTypeBO> censusRegisterTypeBOList) throws ConsoleException
	{
		logger.info("====删除户口类别信息列表开始====");
		logger.debug("====删除户口类别信息列表.入参:List<CensusRegisterTypeBO>＝[{}]======", censusRegisterTypeBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (CensusRegisterTypeBO itemBO : censusRegisterTypeBOList)
		{
			ConsoleReturnBO itemResult = removeCensusRegisterType(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====删除户口类别信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除户口类别信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countCensusRegisterType(CensusRegisterTypeBO censusRegisterTypeBO)
	{
		logger.info("====统计户口类别信息开始====");
		logger.debug("====统计户口类别信息。入参：CensusRegisterTypeBO＝[{}]======", censusRegisterTypeBO);
		Long returnLong = new Long(0);
		CensusRegisterTypePO censusRegisterTypePO = new CensusRegisterTypePO();
		copyCensusRegisterTypeBO2CensusRegisterTypePO(censusRegisterTypeBO, censusRegisterTypePO);
		returnLong = censusRegisterTypeDao.count(censusRegisterTypePO);
		logger.debug("====统计户口类别信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计户口类别信息结束====");
		return returnLong;
	}

	private void copyCensusRegisterTypeBO2CensusRegisterTypePO(CensusRegisterTypeBO censusRegisterTypeBO, CensusRegisterTypePO censusRegisterTypePO)
	{
		beanUtils.copyBeanNotNull(censusRegisterTypeBO, censusRegisterTypePO);
		if (censusRegisterTypeBO.getSysID() != null)
		{
			censusRegisterTypePO.setSeqID(censusRegisterTypeBO.getSysID());
		}
	}

	private void copyCensusRegisterTypePO2CensusRegisterTypeBO(CensusRegisterTypePO censusRegisterTypePO, CensusRegisterTypeBO censusRegisterTypeBO)
	{
		beanUtils.copyBeanNotNull(censusRegisterTypePO, censusRegisterTypeBO);
		if (censusRegisterTypePO.getSeqID() != null)
		{
			censusRegisterTypeBO.setSysID(censusRegisterTypePO.getSeqID());
		}
	}

}
